package Utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Classname Utils
 * @Description TODO
 * @Date 2021/8/12 20:37
 * @Created by xx
 */
public class Utils {

    private static SimpleDateFormat MM_DD = new SimpleDateFormat("yyyy-MM-dd");

    public static Boolean IsEmpty(Object o){
        return o==null || o.toString().isEmpty();
    }


    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};


    /**
     * sql条件拼接示例
     * @param sqlBuilder sql
     * @param appendCondition
     * @param field
     * @param fieldAppend
     * @param obj
     */
    public static void appendCommon(StringBuilder sqlBuilder,String appendCondition,String field,String fieldAppend,Object obj){
        String sql=sqlBuilder.toString().toUpperCase();
        if(!sql.contains("WHERE")){
            appendCondition="WHERE";
        }
        sqlBuilder.append(" ").append(appendCondition)
                .append(" ");
        fieldAppend=fieldAppend.toUpperCase();
        if("LIKE".equals(fieldAppend)){
            sqlBuilder.append(field).append(" LIKE '%").append(obj).append("%'");
        }else {
            sqlBuilder.append(field).append(" ").append(fieldAppend)
                    .append("'").append(obj).append("'");
        }
    }



    /**
     * 分页拼接
     * @param stringBuilder
     * @param page
     * @param size
     */
    public static void appendPageSql(StringBuilder stringBuilder,Integer page,Integer size){
        String sql=stringBuilder.toString().toUpperCase();
        if(!sql.contains("LIMIT")){
            page=(page-1)*size;
            stringBuilder.append("LIMIT ").append(page).append(" ,").append(size);
        }
    }


    /**
     * 获取Date 对象是星期几
     * @param date 日期对象
     * @return 星期几
     */
    public static String getWeekByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        if(date!=null){
            calendar.setTime(date);
        }
        return WEEK_DAYS[calendar.get(Calendar.DAY_OF_WEEK)-1];
    }


    /**
     * 获取当前月份
     * @return 当前月份
     */
    public static Integer getNowMonth(){
        Calendar calendar=Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * 获取当前年份
     * @return 当前年份
     */
    public static Integer getNowYear(){
        Calendar calendar=Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取date中的年份
     * @param date
     * @return
     */
    public static Integer getYearByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }


    /**
     * 获取date中的月份
     * @param date
     * @return
     */
    public static Integer getMonthByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH)+1;
    }





    /**
     * 获取每个月最后的一天
     * @param year
     * @param month
     * @return
     */
    public static String getDateLastDay(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        return MM_DD.format(cal.getTime());
    }





    /**
     * 通过毫秒数获取日期时间
     * @param time  毫秒数
     * @return 日期
     */
    public static Date getDateByLong(Long time){
        Date date=new Date();
        date.setTime(time==null?0:time);
        return date;
    }

    /**
     * 获取日期的毫秒数
     * @param date 日期
     * @return 毫秒数
     */
    public static Long getLongByDate(Date date){
        return date==null?0:date.getTime();
    }





}
